package com.brush.web.controller.sysmenu;


import com.brush.common.annotation.Log;
import com.brush.common.core.controller.BaseController;
import com.brush.common.core.domain.R;
import com.brush.common.enums.BusinessType;
import com.brush.system.domain.req.sysmenu.SysMenuAddReq;
import com.brush.system.domain.req.sysmenu.SysMenuEditReq;
import com.brush.system.domain.req.sysmenu.SysMenuReq;
import com.brush.system.domain.resp.sysmenu.SysMenuResp;
import com.brush.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/admin/menu")
@Api(tags = "后台-菜单管理Api")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;


    @ApiOperation("菜单列表")
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/list")
    public R<List<SysMenuResp>> list(SysMenuReq req) {
        List<SysMenuResp> resp = this.sysMenuService.search(req);
        return R.ok(resp);
    }

    // 根据编号获取菜单详细信息
    @ApiOperation(value = "根据编号获取菜单详细信息")
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping(value = "/{menuId}")
    public R<SysMenuResp> getInfo(@PathVariable("menuId") Long menuId) {
        SysMenuResp sysMenuResp = this.sysMenuService.findById(menuId);
        return R.ok(sysMenuResp);
    }

    // 新增菜单
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增菜单")
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @PostMapping
    public R add(@RequestBody @Valid SysMenuAddReq req) {
        boolean res = this.sysMenuService.add(req);
        return super.isSuccess(res);
    }

    // 修改菜单
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改菜单")
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @PutMapping
    public R edit(@RequestBody @Valid SysMenuEditReq req) {
        boolean res = this.sysMenuService.edit(req);
        return super.isSuccess(res);
    }

    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除菜单")
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @DeleteMapping("/{menuId}")
    public R remove(@PathVariable("menuId") Long menuId) {
        return R.ok(this.sysMenuService.removeById(menuId));
    }

}
